// pages/personnel/user/education/index.js
import Config from "../../../main.config";
import {getList, add, edit} from "../../../service/Education";
import {getStorageSync} from "../../../utils/Storage";
import {getUserInfo} from "../../../service/Common";
import Education from "../../../service/Education";
import Dialog from "@vant/weapp/dialog/dialog";
import {getConfig} from "../../../service/System";

Page({

    /**
     * 页面的初始数据
     */
    data: {
        activeName: "1",
        list: [],
        edit: {
            show: false,
            loading: false
        },
        showAddBtn: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: async function (options) {
        try {
            this.data._options = options;
            this.data._systemConfig = await getConfig("RESUME_NUMBER");
            if (options.todo === "create") {
                await this.getUserInfo();
                this.setData({"edit.show": true});
            } else {
                this.data._userId = getStorageSync(Config.storage.userInfo + ".userId");
                await this.getEducationList();
            }
        } catch (e) {
            console.error(e)
        }
    },

    /*
    * 获取用户信息
    * 如果用户信息存在 且 roleId存在 则该用户已注册
    * 如果不存在 则跳转注册页面进行注册
    * */
    getUserInfo: async function () {
        try {
            let openId = getStorageSync(Config.storage.openId);
            let userInfo = await getUserInfo(openId);
            this.data._userId = userInfo.userId;
            await this.getEducationList();
        } catch (e) {
            console.error(e)
        }
    },

    /*
    * 获取受教育列表
    * */
    getEducationList: async function () {
        let userId = this.data._userId;
        let systemConfig = this.data._systemConfig;
        let list = await await getList(userId);
        let len = list.length;
        this.setData({
            list,
            showAddBtn: len > 0 && len < systemConfig.value
        })
        return list
    },


    /*
    * 面包状态切换
    * */
    onCollapseChange(event) {
        this.setData({
            activeName: event.detail,
        });
    },
    
    /*
    * 保存记录
    * */
    onSave: async function (e) {
        try {
            let todo = this.data._options.todo;
            let postData = e.detail;
            let list = this.data.list;
            let findInx = list.findIndex(function (item) {
                return item.schooling === postData.schooling && postData.id !== item.id
            })
            if (findInx >= 0) {
                await Dialog.alert({
                    title: "提示",
                    message: "不能添加相同学历的教育信息"
                })
                return false
            }
            postData.id ? await edit(postData) : await add(postData);
            if (todo !== "create") { //非首次创建用户
                this.setData({"edit.show": false});
                await this.getEducationList();
            } else {
                wx.reLaunch({
                    url: "/pages/index/index"
                })
            }
        } catch (e) {
            console.error(e)
        } finally {
            this.setData({"edit.buttonLoading": false})
        }
    },

    /*
    * 点击操作按钮
    * */
    onButtonClick: async function (e) {
        try {
            let list = this.data.list;
            let name = e.currentTarget.dataset.name;
            let index = e.currentTarget.dataset.index;
            let currentData = list[index];
            if (name === "edit") {
                this.setData({
                    "edit.show": true,
                    "edit.info": currentData
                })
            } else {
                await Dialog.confirm({
                    title: "提示",
                    message: "是否删除该条数据？"
                })
                await Education.del(currentData.id);
                let list = await this.getEducationList();
                this.setData({"edit.show": list.length === 0})
            }
        } catch (e) {
            console.error(e)
        }
    },

    /*
    * 点击添加按钮
    * */
    onAddButton: async function () {
        this.setData({
            "edit.show": true,
            "edit.info": {},
            showAddBtn: false
        });
    }
})